//
//  JCMessageTextContentView.swift
//  JChat
//
//  Created by JIGUANG on 2017/3/9.
//  Copyright © 2017年 HXHG. All rights reserved.
//

import UIKit
import MTCategory

open class JCClubActivityContentView: UIView, JCMessageContentViewType {

    
    private weak var _delegate: JCMessageDelegate?
    private var _message: JCMessageType!
    public override init(frame: CGRect) {
        super.init(frame: frame)
        _commonInit()
    }
    public required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        _commonInit()
    }

    open override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
        return super.canPerformAction(action, withSender: sender)
    }
    
    open func apply(_ message: JCMessageType) {
        guard let content = message.content as? JCClubActivityContent else {
            return
        }
        
        
        let _isSelf = (message.sender?.username == JMSGUser.myInfo().username)
        _delegate = content.delegate
        _message = message
        //"[\"messageType\": activity, \"activityInfo\": Jxjxjznn , \"sendUserHeadUrl\": 打SEI勒, \"activityId\": 25, \"activityName\": 技术监督局多久, \"sendUserNickname\": mitu_IqkMmGYt]"
        if let _extras = content.infos {
            // 头像设置, nickName
            if let sendUserNickname: String = _extras["sendUserNickname"] as? String {
                JMSGUser.userInfoArray(withUsernameArray: [sendUserNickname]) { (result, error) in
                    if error == nil {
                        let users = result as! [JMSGUser]
                        let user = users.first
//                        user?.thumbAvatarData({ (data, id, error) in
//                            if let data = data {
//                                let image = UIImage(data: data)
//                                self.avatorImgV.image = image
//                            } else {
//                                self.avatorImgV.image = UIImage.loadImage("com_icon_user_36")
//                            }
//                        })
                        self.avatorImgV.mtSetImage(user?.avatar, nil)
                        self.nickNameLabel.text = user?.displayName()
                    }
                }
            }
            
            if let activityName: String = _extras["activityName"] as? String {
                self.activityNameLabel.text = activityName
            }
            
            if let activityInfo: String = _extras["activityInfo"] as? String {
                
                let mattr = NSMutableAttributedString(string: activityInfo)
                
                let paraStyle = NSMutableParagraphStyle()
                
                paraStyle.lineSpacing = 5
                
                let attributes: [NSAttributedString.Key : Any] = [NSAttributedString.Key.font : PingFangSCRegularFont(12),
                                                                   NSAttributedString.Key.paragraphStyle : paraStyle]
                
                mattr.addAttributes(attributes, range: NSMakeRange(0, mattr.length))
                
                self.activityDescLabel.attributedText = mattr
                
            }
            
        }
        tipDescBgView.coRoundingCorners(corners: [.topRight, .bottomRight], radii: 11)
        tipDescBgView.setNeedsLayout()
        tipDescBgView.layoutIfNeeded()
    }
    
    private func _commonInit() {
        setupUI()
        makeConstraint()
        _tapGesture()
    }
    
    private func setupUI() {
        addSubview(bgColorView)
        
        bgColorView.addSubview(avatorImgV)
        bgColorView.addSubview(nickNameLabel)
        bgColorView.addSubview(activityNameLabel)
        
        bgColorView.addSubview(bgDescView)
        
        bgDescView.addSubview(tipDescBgView)
        
        tipDescBgView.addSubview(tipDescLabel)
        
        bgDescView.addSubview(activityDescLabel)
    }
    
    private func makeConstraint() {
        bgColorView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        avatorImgV.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(9)
            make.left.equalToSuperview().offset(11)
            make.width.height.equalTo(22)
        }
        
        nickNameLabel.snp.makeConstraints { (make) in
            make.centerY.equalTo(avatorImgV)
            make.left.equalTo(avatorImgV.snp.right).offset(4)
            make.right.equalToSuperview().offset(-11)
        }
        
        activityNameLabel.snp.makeConstraints { (make) in
            make.left.equalTo(avatorImgV)
            make.right.equalToSuperview().offset(-11)
            make.top.equalTo(avatorImgV.snp.bottom).offset(8)
        }
        
        bgDescView.snp.makeConstraints { (make) in
            make.top.equalTo(activityNameLabel.snp.bottom).offset(10)
            make.left.right.equalTo(activityNameLabel)
            make.bottom.equalToSuperview().offset(-12)
        }

        tipDescBgView.snp.makeConstraints { (make) in
            make.left.top.equalToSuperview()
            make.width.equalTo(70)
            make.height.equalTo(22)
        }

        tipDescLabel.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }

        activityDescLabel.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(6)
            make.right.equalToSuperview().offset(-6)
            make.top.equalTo(tipDescLabel.snp.bottom).offset(8)
            make.height.lessThanOrEqualTo(106)
            make.bottom.bottom.equalToSuperview().offset(-10)
        }
        
    }
    
    lazy var bgColorView: UIView = {
        let v = UIView()
        v.backgroundColor = UIColor.theme.main
        v.layer.cornerRadius = 5
        return v
    }()
    
    lazy var avatorImgV: UIImageView = {
        let imageV = UIImageView()
        imageV.layer.cornerRadius = 11
        imageV.layer.masksToBounds = true
        imageV.backgroundColor = .white
        imageV.contentMode = .scaleAspectFill
        return imageV
    }()
    
    lazy var nickNameLabel: UILabel = {
        let l = UILabel()
        l.font = PingFangSCRegularFont(14)
        l.textColor = .black
        l.text =  ""
        return l
    }()
    
    lazy var activityNameLabel: UILabel = {
        let l = UILabel()
        l.font = PingFangSCMediumFont(16)
        l.textColor = .black
        l.text =  ""
        l.numberOfLines = 0
        l.sizeToFit()
        return l
    }()
    
    lazy var bgDescView: UIView = {
        let v = UIView()
        v.backgroundColor = .white
        v.layer.cornerRadius = 5
        return v
    }()
    
    lazy var tipDescBgView: UIView = {
        let v = UIView()
        v.backgroundColor = .black
        //v.coRoundingCorners(corners: [.topRight, .bottomRight], radii: 11)
        return v
    }()
    
    lazy var tipDescLabel: UILabel = {
        let l = UILabel()
        l.font = PingFangSCMediumFont(14)
        l.textColor = .white
        l.text = "活动简介"
        l.textAlignment = .center
        return l
    }()
    
    lazy var activityDescLabel: UILabel = {
        let l = UILabel()
        l.font = PingFangSCRegularFont(12)
        l.textColor = UIColor.redpack.x5d5d5d
        l.text = ""
        l.numberOfLines = 0
        l.sizeToFit()
        return l
    }()
    
    
    
    
    
    
    func _tapGesture() {
        let tap = UITapGestureRecognizer(target: self, action: #selector(_clickCell))
        tap.numberOfTapsRequired = 1
        addGestureRecognizer(tap)
    }
    
    @objc func _clickCell() {
        _delegate?.tapClubActivity?(message: _message)
    }
}


